﻿using AutoMapper;
using ETask.Api.Data;
using ETask.Api.Dtos;
using ETask.Api.Models;
using Microsoft.EntityFrameworkCore;

namespace ETask.Api.Repositories
{
    /// <summary>
    /// 用户仓储实现
    /// </summary>
    public class UserRepository : Repository<User>, IUserRepository
    {

        public UserRepository(AppDbContext context) : base(context)
        {
        }

        public async Task<User> GetByAccount(string account)
        {
            User user = await _context.Users.FirstOrDefaultAsync(q => q.Account.Equals(account));
            return user;
        }

        public async Task<UserDto> GetByInfoByAccount(string account)
        {
            var query = _context.Users
                .Where(u => u.Account == account)
                .GroupJoin(
                    _context.Depts,
                    user => user.DeptCode,
                    dept => dept.DeptCode,
                    (user, deptGroup) => new { user, deptGroup }
                )
                .SelectMany(
                    x => x.deptGroup.DefaultIfEmpty(),
                    (x, dept) => new UserDto
                    {
                        Id = x.user.Id,
                        Account = x.user.Account,
                        Name = x.user.Name,
                        Phone = x.user.Phone,
                        Email = x.user.Email,
                        Sex = x.user.Sex,
                        DeptCode = x.user.DeptCode,
                        DeptName = dept == null ? "无部门" : dept.DeptName,
                        CreatedAt = x.user.CreatedAt,
                        UpdatedAt = x.user.UpdatedAt,
                    }
                );

            return await query.FirstOrDefaultAsync();
        }

        public async Task<PagedResult<UserDto>> GetListAsync(UserListParams listParams)
        {
            var baseQuery = _context.Users
                .GroupJoin(
                    _context.Depts,
                    user => user.DeptCode,
                    dept => dept.DeptCode,
                    (user, deptGroup) => new { user, deptGroup }
                )
                .SelectMany(
                    x => x.deptGroup.DefaultIfEmpty(),
                    (x, dept) => new { user = x.user, dept }
                );

            // 应用搜索条件
            if (!string.IsNullOrWhiteSpace(listParams.KeyWord))
            {
                baseQuery = baseQuery.Where(q =>
                    q.user.Account.Contains(listParams.KeyWord) ||
                    q.user.Name.Contains(listParams.KeyWord) ||
                    (q.dept != null && q.dept.DeptName.Contains(listParams.KeyWord)));
            }

            if (!string.IsNullOrWhiteSpace(listParams.DeptCode))
            {
                baseQuery = baseQuery.Where(q =>
                    q.dept != null &&
                    q.dept.DeptCode.StartsWith(listParams.DeptCode));
            }
            // 分页查询

            var res = await baseQuery
                .Select(q => new UserDto
                {
                    Id = q.user.Id,
                    Account = q.user.Account,
                    Name = q.user.Name,
                    Phone = q.user.Phone,
                    Email = q.user.Email,
                    IsAdmin = q.user.IsAdmin,
                    Sex = q.user.Sex,
                    DeptCode = q.user.DeptCode,
                    DeptName = q.dept == null ? "无部门" : q.dept.DeptName,
                    CreatedAt = q.user.CreatedAt,
                    UpdatedAt = q.user.UpdatedAt,
                }).ToPagedResultAsync<UserDto>(listParams.PageDto);
            return res;
        }
    }
}
